# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import json
from common.utils.config import ConfigUtil
from apps.admin.schemas.setting import censor_schema as schema


class CensorService:
    """ 审查配置服务类 """

    @classmethod
    async def detail(cls) -> schema.CensorDetailVo:
        """
        审查配置详情。

        Returns:
            schema.CensorDetailVo: 审查配置详情Vo。

        Author:
            zero
        """
        conf = await ConfigUtil.get("content_censor") or {}
        baidu = conf.get("baidu") or {}
        aliyun = conf.get("aliyun") or {}
        return schema.CensorDetailVo(
            channel=conf.get("channel", "") or "baidu",
            review_question=bool(int(conf.get("review_question") or 0)),
            review_draws=bool(int(conf.get("review_draws") or 0)),
            review_video=bool(int(conf.get("review_video") or 0)),
            review_music=bool(int(conf.get("review_music") or 0)),
            review_users=bool(int(conf.get("review_users") or 0)),
            review_texts=bool(int(conf.get("review_texts") or 0)),
            review_image=bool(int(conf.get("review_image") or 0)),
            baidu_app_id=baidu.get("app_id") or "",
            baidu_api_key=baidu.get("api_key") or "",
            baidu_secret_key=baidu.get("secret_key") or "",
            aliyun_api_key=aliyun.get("access_key_id") or "",
            aliyun_secret_key=aliyun.get("access_key_secret") or ""
        )

    @classmethod
    async def save(cls, post: schema.CensorDetailVo):
        """
        审查配置保存。

         Args:
            post (schema.CensorDetailVo): 审查配置参数。

        Author:
            zero
        """
        baidu = json.dumps({
            "app_id": post.baidu_app_id.strip(),
            "api_key": post.baidu_api_key.strip(),
            "secret_key": post.baidu_secret_key.strip()
        }, ensure_ascii=False)

        aliyun = json.dumps({
            "access_key_id": post.aliyun_api_key.strip(),
            "access_key_secret": post.aliyun_secret_key.strip()
        }, ensure_ascii=False)

        await ConfigUtil.set("content_censor", "channel", post.channel)
        await ConfigUtil.set("content_censor", "review_question", int(post.review_question))
        await ConfigUtil.set("content_censor", "review_draws", int(post.review_draws))
        await ConfigUtil.set("content_censor", "review_video", int(post.review_video))
        await ConfigUtil.set("content_censor", "review_music", int(post.review_music))
        await ConfigUtil.set("content_censor", "review_users", int(post.review_users))
        await ConfigUtil.set("content_censor", "review_texts", int(post.review_texts))
        await ConfigUtil.set("content_censor", "review_image", int(post.review_image))
        await ConfigUtil.set("content_censor", "baidu", baidu)
        await ConfigUtil.set("content_censor", "aliyun", aliyun)
